package com.video.netty.InChannelHandler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.video.commons.utils.TaskUtil;
import com.video.entity.Solution;
import com.video.entity.Task;
import com.video.netty.commons.utils.ChannelUtil;
import com.video.service.task.ITaskService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * {"id":"getSolutionTask", "solutionname":"夏季作息"，
 *  "account":"admin", "msgid": "xxxxxxxxxxxxx"}
 * 返回
 * {"id":"getSolutionTaskResponse",
 * "msgid": "xxxxxxxxxxxxx",
 * "status":"succ",
 * "response":"success",
 * "account":"admin",
 * "taskPriority":0,
 * "tasklist":[{"solution":"夏季作息", "taskname":"Region","taskPriority":0,"loopType":1,"stopDate":"2019-06-10","volume":0,"mp3":["1.mp3","2.mp3"],"startTime":"16:22:53","duration":1800,"playOrder":1,"users":["1"],"status":0,"taskid":"11","samll":0,"startDate":"2018-12-14","weekMask":254}]}
 * {"id":"getSolutionTaskResponse", "msgid": "xxxxxxxxxxxxx", "status":"fail", "response":"fail","account":"admin", "tasklist":[]}
 */
@BaseChannelHandler.MsgHandler
public class WsGetSolutionTaskChannleHandler extends BaseChannelHandler {

    @Autowired
    private ITaskService taskService;

    @Autowired
    private ChannelUtil channelUtil;

    @Override
    protected String msgId() {
        return "getSolutionTask";
    }

    @Override
    protected void msgHandler(JSONObject json) throws Exception {
        //获得方案对象
        Solution solution = JSON.parseObject(json.toJSONString(), Solution.class);
        List<Task> tasks = taskService.queryBySolution(solution.getSolutionname(), solution.getAccount());

        StringBuilder sb = new StringBuilder();
        sb.append("{\"id\":\"getSolutionTaskResponse\", \"response\":\"success\",\"account\":\"" + solution.getAccount() + "\",\"tasklist\":");
        if(tasks != null){
            List<Map<String, Object>> list = new ArrayList<>();
            for (Task task : tasks) {
//                Map map = new HashMap();
//                map.put("solution", task.getSolution());
//                map.put("taskname", task.getTaskname());
//                map.put("taskPriority", task.getTaskpriority());
//                map.put("loopType", task.getLooptype());
//                map.put("stopDate", task.getStopDate());
//                map.put("volume", task.getVolume());
//                map.put("mp3", JSON.parseArray(task.getMp3(), String.class));
//                map.put("startTime", task.getStartTime());
//                map.put("duration", task.getDuration());
//                map.put("playOrder", task.getPlayOrder());
//                map.put("users", JSON.parseArray(task.getUsers(), String.class));
//                map.put("taskid", task.getTaskid());
//                map.put("status", task.getStatus());
//                map.put("samll", task.getSamll());
//                map.put("startDate", task.getStartDate());
//                map.put("weekMask", task.getWeekMask());

                Map map = TaskUtil.task2Map(task);
                list.add(map);
            }
            sb.append(JSON.toJSONString(list));
        } else {
            sb.append("[]");
        }
        sb.append("}");

        channelUtil.sendMsg(sb.toString());
    }

    @Override
    protected void exceptionHandler(JSONObject json, Exception e) {
        String error = "{\"id\":\"getSolutionTaskResponse\", \"response\":\"fail\",\"account\":\"" + json.getString("account") + "\"}";
        channelUtil.sendMsg(error);
    }
}
